$().ready(function() {
	validateRule();
});

$.validator.setDefaults({
	submitHandler : function() {
		save();
	}
});

var $button = $('#upload'),
//选择文件按钮
$file = $("#files"),
//回显的列表
$list = $('.file-list'),
//选择要上传的所有文件
fileList = [];
//当前选择上传的文件
var curFile;
// 选择按钮change事件，实例化fileReader,调它的readAsDataURL并把原生File对象传给它，
// 监听它的onload事件，load完读取的结果就在它的result属性里了。它是一个base64格式的，可直接赋值给一个img的src.
$file.on('change', function (e) {
//上传过图片后再次上传时限值数量
var numold = $('li').length;
if(numold >= 10){
    layer.alert('最多上传10张图片');
    return;
}
//限制单次批量上传的数量
var num = e.target.files.length;
var numall = numold + num;
if(num >10 ){
   layer.alert('最多上传10张图片');
   return;
}else if(numall > 10){
    layer.alert('最多上传10张图片');
    return;
}
//原生的文件对象，相当于$file.get(0).files;//files[0]为第一张图片的信息;
curFile = this.files;
//curFile = $file.get(0).files;
//console.log(curFile);
//将FileList对象变成数组
fileList = fileList.concat(Array.from(curFile));
//console.log(fileList);
for (var i = 0, len = curFile.length; i < len; i++) {
    reviewFile(curFile[i])
}
$('.file-list').fadeIn(100);
})


function reviewFile(file) {
//实例化fileReader,
var fd = new FileReader();
//获取当前选择文件的类型
var fileType = file.type;
//调它的readAsDataURL并把原生File对象传给它，
fd.readAsDataURL(file);//base64
//监听它的onload事件，load完读取的结果就在它的result属性里了
	fd.onload = function () {
		if (/^image\/[jpeg|png|jpg|gif|jpg]/.test(fileType)) {
		
			$list.append('<li id = "img" style="border:solid red px; margin:5px 5px;" class="file-item"><img src="' + this.result + '" alt="" height="150"><span class="file-del">删除 </span><span class="file-radio"class="">选中 <input  type="radio" id="status" name="status" onclick="' + this.result + '" value=="' + this.result + '"  /></span></li>').children(':last').hide().fadeIn(100);
	
		} else {
			$list.append('<li id = "D99" class="file-item"><span class="file-name">' + file.name + '</span><span  id = "D99" class="file-del">删除</span></li>')
			layer.msg("请您选择有效的图片格式！");

		}

	}
}

//点击选中事件：
$(".file-list").on('click', '.file-radio', function (id) {
	let $parent = $(this).parent()
	console.log($parent);
	let index = $parent.index();
	$('#indexradio').val(index);
});

//点击删除按钮事件：
$(".file-list").on('click', '.file-del', function () {
	let $parent = $(this).parent()
	console.log($parent);
	let index = $parent.index();

	fileList.splice(index, 1);
	$parent.fadeOut(100, function () {
		$parent.remove()
	});
});


function save() {
	
	  
	
	  var formData = new FormData();
	  
    for (var i = 0, len = fileList.length; i < len; i++) {
        //console.log(fileList[i]);
        formData.append('files', fileList[i]);
    }
    
    
	  //文件索引
	  var indexradio=$("#indexradio").val();
	  formData.append('indexradio', indexradio);   
    
	  var officeName=$("#officeName").val();
	  formData.append('officeName', officeName);
	  
	  var officeAdd=$("#officeAdd").val();
	  formData.append('officeAdd', officeAdd);
	  
	  var officeAtm=$("#officeAtm").val();
	  formData.append('officeAtm', officeAtm);
	  
	  var officeRanking=$("#officeRanking").val();
	  formData.append('officeRanking', officeRanking);
	 
	  var officeKbn=$("#officeKbn").val();
	  formData.append('officeKbn', officeKbn);
	  
	  var createBy=$("#userId").val();
	  formData.append('createBy', createBy);
	  //
	  
	  var builtArea=$("#builtArea").val();
	  formData.append('builtArea', builtArea);
	  
	  var standardHeight=$("#standardHeight").val();
	  formData.append('standardHeight', standardHeight);
	  
	  var totalFloor=$("#totalFloor").val();
	  formData.append('totalFloor', totalFloor);
	  
	  var roomRate=$("#roomRate").val();
	  formData.append('roomRate', roomRate);
	  
	  var jundate=$("#jundate").val();
	  formData.append('jundate', jundate);
	  
	  var developers=$("#developers").val();
	  formData.append('developers', developers);
	  
	  var propertyCompany=$("#propertyCompany").val();
	  formData.append('propertyCompany', propertyCompany);
	  var describeinfo=$("#describeinfo").val();
	  formData.append('describeinfo', describeinfo);
	    $.ajax({
	    	url : "/xzl/buildingManagement/save",
	        type: 'post',
	        data: formData,
	        processData: false,
	        contentType: false,
	        success: function (data) {
	        	layer.msg("操作成功");
	        	document.getElementById("signupForm").reset();
	        	$("li").remove();
	        	fileList = []
	            parent.reLoad();
	            var index = parent.layer.getFrameIndex(window.name); // 获取窗口索引
	            parent.layer.close(index);
	        }
	    })
}


function validateRule() {
	var icon = "<i class='fa fa-times-circle'></i> ";
	$("#signupForm").validate({
		rules : {
			officeAdd : {
				required : true,
				maxlength:99
			},
			officeName : {
				required : true,
				maxlength:99
			},
			officeAtm : {
				required : true,
				maxlength:15,
				number: true
			},
			officeRanking : {
				maxlength:11
			},
			officeKbn : {
				maxlength:11
			},
			builtArea : {
				required : true,
				maxlength:10,
				number: true
			},
			standardHeight : {
				required : true,
				maxlength:10,
				number: true
			},
			totalFloor : {
				required : true,
				maxlength:10,
				number: true
			},
			roomRate : {
				required : true,
				maxlength:10,
				number: true
			},
			jundate : {
				required : true
			},
			developers : {
				required : true,
				maxlength:100
			},
			propertyCompany : {
				required : true,
				maxlength:100
			},
			describeinfo : {
				maxlength:400
			}
			
		},
		messages : {
			name : {
				required : icon + "请输入姓名"
			}
		}
	})
}